home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / javnl012.zip / JAVNL012.TXT < prev   
Text File  |  1996-12-09  |  17KB  |  513 lines

  1. Issue #012
  2. December, 1996
  3.  
  4.  
  5. Contents:
  6.  
  7. Jack
  8. Comparing C/C++ and Java Part 12 - Null
  9. JavaScript
  10. Introduction to Applet Programming Part 8 - Client/Server Example
  11. Correction
  12.  
  13.  
  14. JACK
  15.  
  16. Lex and Yacc are widely-used tools for doing lexical scanning and
  17. parsing of input.  You describe the structure of the input and these
  18. tools generate programs (in C) that will accept and structure such
  19. input.  For example, an identifier might be lexically described as:
  20.  
  21.         [a-zA-Z_][a-zA-Z_0-9]*
  22.  
  23. meaning that the identifier starts with a letter or underscore,
  24. followed by zero or more letters, underscores, or digits.
  25.  
  26. Similarly, a simple expression in BNF (Backus Naur Form) could look
  27. like:
  28.  
  29.         E -> T | E + T
  30.  
  31.         T -> F | T * F
  32.  
  33.         F -> number | ( E )
  34.  
  35. This is known as a grammar, and programming languages like Java are
  36. formally described via a grammar.  O'Reilly and others publish books
  37. on Lex/Yacc, if you want information on these tools.
  38.  
  39. Jack is a new tool that combines Lex and Yacc capabilities.  It's
  40. written in Java and produces Java output, that is, produces Java
  41. programs for parsing input described via a grammar.  Jack is available
  42. on the Web at:
  43.  
  44.         http://www.suntest.com/Jack
  45.  
  46. You need the Java Development Kit 1.0.2 to use this program.  With
  47. Jack you describe the lexical and syntactic structure of your program,
  48. and then have it produce Java programs that understand this structure.
  49.  
  50. As an example of a problem made simple by a tool of this type,
  51. consider the issue of extracting from Java programs basic information
  52. about what is defined where, for example, where a specific class or
  53. method is found in the source code.  With Jack, it's simply a matter
  54. of dumping out key information whenever a particular construct like a
  55. method declaration is found.
  56.  
  57. For example, we might have this format for the output:
  58.  
  59.         String.charAt method 204 java/src/java/lang/String.java
  60.  
  61. meaning that the charAt() method of class String is defined at line
  62. 204 of String.java.
  63.  
  64. An index produced in this format, representing all the classes,
  65. interfaces, methods, and constructors for JDK 1.0.2, is available via
  66. FTP at:
  67.  
  68.         ftp://rmi.net/pub2/glenm/jack
  69.  
  70. along with a simple UNIX shell script browser to search and access any
  71. of the symbols in the database.
  72.  
  73. There are many other possibilities for the use of a tool of this type,
  74. for example test coverage or generation of class metrics.  If you have
  75. interest in this area, please send me a note (glenm@glenmccl.com).
  76.  
  77.  
  78. COMPARING C/C++ AND JAVA PART 12 - NULL
  79.  
  80. In C and C++, "NULL" is a constant defined in a header file, with a
  81. value like:
  82.  
  83.         0
  84.  
  85. or:
  86.  
  87.         0L
  88.  
  89. or:
  90.  
  91.         ((void*)0)
  92.  
  93. depending on the compiler and memory model options.  NULL is not,
  94. strictly speaking, part of C/C++ itself.
  95.  
  96. In Java, "null" is not a keyword, but a special literal of the null
  97. type.  It can be cast to any reference type, but not to any primitive
  98. type such as int or boolean.  The null literal doesn't necessarily have
  99. value zero.  And it is impossible to cast to the null type or declare a
  100. variable of this type.
  101.  
  102.  
  103. JAVASCRIPT
  104.  
  105. You may have heard the term "JavaScript".  What is its relation to
  106. Java and HTML?  JavaScript started out as LiveScript and its
  107. connection to Java is fairly loose, sharing some Internet heritage but
  108. not that much else.
  109.  
  110. HTML (Hyper Text Markup Language) is not really a programming language
  111. in the usual sense of the term, but rather a language that describes
  112. what a page ought to look like.  You can't really "do anything" with
  113. HTML.
  114.  
  115. Java is a general purpose programming language.  One type of Java
  116. program, an applet, is downloadable by a Web browser and then executed
  117. locally by the browser.  This is useful, for example, in communicating
  118. with remote databases.  An applet's invocation is described via the
  119. HTML tags <applet> and </applet>.
  120.  
  121. JavaScript can be viewed as a language something like Perl, or as an
  122. extension of HTML to allow customized functionality.  As an example of
  123. what a JavaScript application would look like, consider this example:
  124.  
  125.         <html>
  126.         <body>
  127.  
  128.         <a href="link1.html">Link #1</a>
  129.         <br>
  130.         <a href="link2.html">Link #2</a>
  131.         <br>
  132.  
  133.         <script>
  134.  
  135.         <!--
  136.  
  137.         function doit()
  138.         {
  139.                 document.write("<br>This page's links are:<br><br>");
  140.                 for (var i = 0; i < document.links.length; i++) {
  141.                         var s = "";
  142.                         s += (i + 1) + ".  ";
  143.                         s += document.links[i];
  144.                         s += "<br>";
  145.                         document.write(s);
  146.                 }
  147.         }
  148.  
  149.         doit()
  150.  
  151.         <!-- -->
  152.  
  153.         </script>
  154.  
  155.         </body>
  156.         <html>
  157.  
  158. This particular application iterates over the links in a page and
  159. displays them in a numbered list:
  160.  
  161.         1.  link1.html
  162.  
  163.         2.  link2.html
  164.  
  165. JavaScript as a programming language has a flavor something like Perl
  166. or Awk, with weak typing and domain-specific system variables like
  167. "document.links[]".
  168.  
  169. Note that the output of a JavaScript script is HTML, so for example we
  170. use "<br>" instead of "\n" to go to the next line.  In the above
  171. example, the function doit() is defined, and then called to produce
  172. HTML.
  173.  
  174. We will probably not say more about JavaScript, but there are a
  175. variety of books available on the topic.  You need a Web browser like
  176. Netscape 3.0 to execute JavaScript programs.
  177.  
  178.  
  179. INTRODUCTION TO APPLET PROGRAMMING PART 8 - CLIENT/SERVER EXAMPLE
  180.  
  181. In this issue we'll show a simple example of client/server
  182. programming.  The client will be an applet and the server a regular
  183. Java program with a main() method in it.
  184.  
  185. In the client/server model, there is a server program running on some
  186. computer.  It accepts connections from clients across the network, and
  187. serves each of them.  For example, the client may be used to accept
  188. some input from a user, which is sent to the server and entered into a
  189. database.  The server can also send information back to the client,
  190. such as an acknowledgment.
  191.  
  192. Server and client communicate via what is known as a socket.  More
  193. technically, a socket is an endpoint of a communications channel, and
  194. thus there is a socket on each end of the channel, one socket for the
  195. server and one for the client.
  196.  
  197. Given a socket, it's possible to do conventional Java I/O between
  198. server and client.
  199.  
  200. Let's now look at the programs:
  201.  
  202.         // server.java
  203.         
  204.         import java.io.*;
  205.         import java.net.*;
  206.         
  207.         public class server extends Thread {
  208.         
  209.                 public static final int DEF_PORT = 1234;// port
  210.                 private int port;
  211.                 private ServerSocket listen;            // server socket
  212.         
  213.                 public server()
  214.                 {
  215.                         // set up the server socket port
  216.         
  217.                         port = DEF_PORT;
  218.                         try {
  219.                                 listen = new ServerSocket(port);
  220.                         }
  221.                         catch (IOException e) {
  222.                                 System.err.println("socket creation error");
  223.                                 System.exit(1);
  224.                         }
  225.         
  226.                         // start the server thread running
  227.         
  228.                         start();
  229.                 }
  230.         
  231.                 public void run()
  232.                 {
  233.                         try {
  234.                                 // accept connections and process them
  235.         
  236.                                 for (;;) {
  237.                                         Socket cs = listen.accept();
  238.                                         connection c = new connection(cs);
  239.                                 }
  240.                         }
  241.                         catch (IOException e) {
  242.                                 System.err.println("connection error");
  243.                         }
  244.                 }
  245.         
  246.                 public static void main(String[] args)
  247.                 {
  248.                         new server();
  249.                 }
  250.         }
  251.         
  252.         class connection extends Thread {
  253.         
  254.                 private Socket client;                  // client socket
  255.                 private DataInputStream in;             // input from socket
  256.                 private PrintStream out;                // output to socket
  257.         
  258.                 public connection(Socket cs)
  259.                 {
  260.                         // set up an individual connection
  261.         
  262.                         try {
  263.                                 client = cs;
  264.                                 InputStream is = client.getInputStream();
  265.                                 in = new DataInputStream(is);
  266.                                 OutputStream os = client.getOutputStream();
  267.                                 out = new PrintStream(os);
  268.                         }
  269.                         catch (IOException e) {
  270.                                 try {
  271.                                         client.close();
  272.                                 }
  273.                                 catch (IOException ee) {
  274.                                         System.err.println("close error");
  275.                                 }
  276.                                 System.err.println("socket stream error");
  277.         
  278.                                 return;
  279.                         }
  280.         
  281.                         // start it running
  282.         
  283.                         start();
  284.                 }
  285.         
  286.                 public void run()
  287.                 {
  288.         
  289.                         // read from socket input and write back to output
  290.         
  291.                         try {
  292.                                 for (;;) {
  293.                                         String ln = in.readLine();
  294.                                         if (ln == null)
  295.                                                 break;
  296.                                         if (ln.length() == 0)
  297.                                                 out.println("empty input");
  298.                                         else
  299.                                                 out.println("OK: " + ln);
  300.                                 }
  301.                         }
  302.                         catch (IOException e) {
  303.                                 System.err.println("server I/O error");
  304.                         }
  305.         
  306.                         // close connection
  307.         
  308.                         finally {
  309.                                 try {
  310.                                         client.close();
  311.                                 }
  312.                                 catch (IOException ee) {
  313.                                         System.err.println("close error");
  314.                                 }
  315.                         }
  316.                 }
  317.         }
  318.  
  319.         // client.java
  320.         
  321.         import java.applet.*;
  322.         import java.awt.*;
  323.         import java.io.*;
  324.         import java.net.*;
  325.         
  326.         public class client extends Applet {
  327.         
  328.                 public static final int DEF_PORT = 1234;// port
  329.                 Socket s;                               // socket
  330.                 DataInputStream in;                     // socket input
  331.                 private PrintStream out;                // socket output
  332.                 TextField input_field;                  // input field
  333.                 TextArea out_area;                      // output display area
  334.         
  335.                 public void init()
  336.                 {
  337.                         try {
  338.                                 // set up socket
  339.         
  340.                                 String host = getCodeBase().getHost();
  341.                                 s = new Socket(host, DEF_PORT);
  342.                                 in = new DataInputStream(s.getInputStream());
  343.                                 out = new PrintStream(s.getOutputStream());
  344.         
  345.                                 // set up window
  346.         
  347.                                 input_field = new TextField();
  348.                                 out_area = new TextArea();
  349.                                 out_area.setEditable(false);
  350.                                 setLayout(new BorderLayout());
  351.                                 add("North", input_field);
  352.                                 add("Center", out_area);
  353.                         }
  354.                         catch (IOException e) {
  355.                                 System.err.println("exception during setup");
  356.                         }
  357.                 }
  358.         
  359.                 public boolean action(Event e, Object o)
  360.                 {
  361.                         if (e.target == input_field) {
  362.         
  363.                                 // we have some input from the user
  364.         
  365.                                 try {
  366.         
  367.                                         // dump it to the server
  368.         
  369.                                         out.println((String)e.arg);
  370.                                         input_field.setText("");
  371.         
  372.                                         // read response
  373.         
  374.  
  375.                                         String status = in.readLine();
  376.                                         out_area.setText(status);
  377.         
  378.                                         return true;
  379.                                 }
  380.                                 catch (IOException ee) {
  381.                                         out_area.setText("server I/O error");
  382.                                 }
  383.                         }
  384.                         return false;
  385.                 }
  386.         }
  387.  
  388. The server is compiled as usual and simply started as a Java program:
  389.  
  390.         $ java server
  391.  
  392. while the client is driven via some HTML:
  393.  
  394.         <html>
  395.         
  396.         <head>
  397.         <title>Client Applet</title>
  398.         </head>
  399.         
  400.         <body>
  401.         
  402.         <applet code="client.class" width=300 height=300></applet>
  403.         
  404.         </body>
  405.         
  406.         </html>
  407.  
  408. If you start the client without the server being present, it will give
  409. an error because of failure to connect to the server.
  410.  
  411. This particular server/client combination simply validates input from
  412. the client and sends back an acknowledgment.  Each client connection
  413. runs as a separate thread, so that many clients can be served
  414. simultaneously without the need for polling.
  415.  
  416. Note that the server knows to close a given client connection (the
  417. client has gone away) by receipt of a "null" when reading input from
  418. that connection.  By contrast, the server "never" goes away; it must
  419. be explicitly terminated by the user.
  420.  
  421. Note also that the socket port must be unique on the system running
  422. the server, and that the server and client must agree on port
  423. assignments.
  424.  
  425.  
  426. CORRECTION
  427.  
  428. In issue #011 we presented an example of animation.  Steve Drach
  429. pointed out that a call to dispose() is needed, as follows:
  430.         
  431.         // run a thread
  432.         public void run()
  433.         {
  434.                 for (;;) {
  435.         
  436.                         // get graphics for the applet window
  437.         
  438.                         Graphics g = this.getGraphics();
  439.                         try {
  440.                                 switch (st++) {
  441.         
  442.                                 case 0:
  443.                                 g.setColor(Color.red);
  444.                                 g.fillOval(25, 35, 250, 250);
  445.                                 break;
  446.         
  447.                                 // other switch cases ...
  448.         
  449.                                 }
  450.         
  451.                                 // sleep for a second
  452.         
  453.                                 Thread.sleep(1000);
  454.                         }
  455.         
  456.                         catch (InterruptedException e) {
  457.                         }
  458.  
  459.                         g.dispose();    // <<<<<<<<<<<<<<<<<<<<
  460.                 }
  461.         }
  462.  
  463. Without this, the animation will eventually stall out.  This is
  464. different from garbage collection, which is automatic, and involves
  465. freeing of windows resources.  That is, getGraphics() retrieves one of
  466. a limited number of system window resources, which must be returned
  467. via dispose().  A similar case might come up when dealing with a
  468. resource such as UNIX file descriptors.
  469.  
  470.  
  471. ACKNOWLEDGEMENTS
  472.  
  473. Thanks to Jay Burgess, Thierry Ciot, Irv Kanode, Mike McCann, Mike
  474. Paluka, Srihari Sampathkumar, and Bob Shore for help with proofreading.
  475.  
  476.  
  477. SUBSCRIPTION INFORMATION / BACK ISSUES
  478.  
  479. To subscribe to the newsletter, send mail to majordomo@world.std.com
  480. with this line as its message body:
  481.  
  482. subscribe java_letter
  483.  
  484. Back issues are available via FTP from:
  485.  
  486.         rmi.net /pub2/glenm/javalett
  487.  
  488. or on the Web at:
  489.  
  490.         http://rainbow.rmi.net/~glenm
  491.  
  492. There is also a C++ newsletter.  To subscribe to it, say:
  493.  
  494. subscribe c_plus_plus
  495.  
  496. using the same majordomo@world.std.com address.
  497.  
  498. -------------------------
  499.  
  500. Copyright (c) 1996 Glen McCluskey.  All Rights Reserved.
  501.  
  502. This newsletter may be further distributed provided that it is copied
  503. in its entirety, including the newsletter number at the top and the
  504. copyright and contact information at the bottom.
  505.  
  506. Glen McCluskey & Associates
  507. Professional Computer Consulting
  508. Internet: glenm@glenmccl.com
  509. Phone: (800) 722-1613 or (970) 490-2462
  510. Fax: (970) 490-2463
  511. FTP: rmi.net /pub2/glenm/javalett (for back issues)
  512. Web: http://rainbow.rmi.net/~glenm
  513.